const app = getApp()
const api = require('../../../utils/api')
const layer = require('../../../utils/layer')
const util = require('../../../utils/util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        customBar: app.globalData.CustomBar, // 顶部工具栏距离手机顶部的距离
        teamName: "",
        id: '0',
        users: [], // 查询出来的用户列表
        nickName: "", // 用户名称
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            teamName: options.teamName,
            id: options.id
        });
    },
    /**
     * 通过名称查询用户列表
     */
    searchUsers() {
        let nickName = this.data.nickName
        let teamId = this.data.id;
        let that = this;
        if (!util.isEmpty(nickName)) {
            api.get("/user/search", { nickName,teamId}).then(res => {
                that.setData({
                    users: res.data
                });
            });
        }
    },
    /**
     * 邀请加入团队
     */
    inviteUser(e) {
        let userId = e.target.dataset.id;
        let teamId = this.data.id;
        let teamName = this.data.teamName;
        let nickName = e.target.dataset.nickName; // 用户昵称
        let that = this;
        layer.showModal(() => {
            api.post("/team/invite", {
                receiveUserId: userId,
                teamId: teamId
            }).then(res => {
                layer.showToast(res.message);
                setTimeout(()=>{
                    that.searchUsers();
                },300);
            });
        }, '提示', `是否邀请【${nickName}】加入【${teamName}】`);
    }
})